package com.shopping_assistant.view;

import java.util.List;

import com.shopping_assistant.model.Person;
import com.shopping_assistant.model.PersonController;
import com.shopping_assistant.model.SimulatedDataProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginView extends Activity {
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_view);
        
        SimulatedDataProvider.simulateData();
        
        Button loginBtn = (Button)findViewById(R.id.loginButton);
        loginBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				boolean loginSuccess = false;
				EditText username = (EditText)findViewById(R.id.usernameEditText);
				EditText password = (EditText)findViewById(R.id.passwordEditText);
				List<Person> persons = PersonController.getInstance().getPersons();
				
				for(Person object : persons) {
					if(object.getUsername().equals(username.getText().toString())) {
						if(object.getPassword().equals(password.getText().toString())) {
							loginSuccess = true;
							PersonController.getInstance().setActiveUser(object);
							
							Intent homeIntent = new Intent(LoginView.this,HomeView.class);
							startActivity(homeIntent);
							break;
						}
					}
				}
				//TODO: Hvis bruger ikke fundet: Skriv i dialogboks at brugeren ikke findes
				if(!loginSuccess) {
					Toast.makeText(getApplicationContext(), "Wrong username and/or password. Please try againg.", Toast.LENGTH_LONG).show();
				}
				
			}
		});
    }
}
